package appita.general;

import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TableRow;

public class LivelloUno extends Activity {

	private ImageView[] arrayLettere = new ImageView[21];
	private int mHeight;
	private int mWidth;

	static HashMap<Integer, String> mHashTabletAlph = new HashMap<Integer, String>() {
		{
			put(0, "a");
			put(1, "b");
			put(2, "c");
			put(3, "d");
			put(4, "e");
			put(5, "f");
			put(6, "g");
			put(7, "h");
			put(8, "i");
			put(9, "l");
			put(10, "m");
			put(11, "n");
			put(12, "o");
			put(13, "p");
			put(14, "q");
			put(15, "r");
			put(16, "s");
			put(17, "t");
			put(18, "u");
			put(19, "v");
			put(20, "z");
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.livello_uno);

		TableRow tabrow = (TableRow) findViewById(R.id.table_row_uno);
		TableRow tabrowdue = (TableRow) findViewById(R.id.table_row_due);
		TableRow tabrowtre = (TableRow) findViewById(R.id.table_row_tre);
		TableRow tabrowquattro = (TableRow) findViewById(R.id.table_row_quattro);
		TableRow tabrowcinque = (TableRow) findViewById(R.id.table_row_cinque);

		// ottengo le dimensioni dello schermo
		WindowManager windowManager = (WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE);
		int screenWidth = windowManager.getDefaultDisplay().getWidth();
		int screenHeight = windowManager.getDefaultDisplay().getHeight();
		mWidth = (int) (screenWidth / 5.1);
		mHeight = (int) (screenHeight / 6.6);

		// creo un array che contiene le immagini delle lettere. Mi serve per
		// distribuirle nella table_row

		for (int i = 0; i < 21; i++) {
			ImageView temp = new ImageView(this);
			temp.setTag(mHashTabletAlph.get(i));

			int nameResourceID = getResources().getIdentifier(mHashTabletAlph.get(i), "drawable",
					getApplicationInfo().packageName);
			temp.setImageBitmap(getBitmapResized(nameResourceID));
			arrayLettere[i] = temp;

			arrayLettere[i].setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					Intent intent = new Intent(LivelloUno.this, ElencoParole.class);
					String pkg = getPackageName();
					intent.putExtra(pkg + ".lettera", "Lettera "+v.getTag().toString().toUpperCase());
					startActivity(intent);

				}
			});

		}

		// distribuisco le lettere nella table_row
		for (int i = 0; i <= 20; i++) {
			if (i >= 5 && i < 10) {
				tabrowdue.addView(arrayLettere[i]);
			} else if (i >= 10 && i < 15) {
				tabrowtre.addView(arrayLettere[i]);
			} else if (i >= 15 && i < 20) {
				tabrowquattro.addView(arrayLettere[i]);
			} else if (i == 20) {
				tabrowcinque.addView(arrayLettere[i]);
			} else {
				tabrow.addView(arrayLettere[i]);
			}
		}

	}

	private Bitmap getBitmapResized(int id) {
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(), id);
		return Bitmap.createScaledBitmap(bitmap, mWidth, mHeight, true);
	}
}
